<?php
require_once __DIR__ . '/../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use Medoo\Medoo; 

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare('gp_week', false, false, false, false);
echo " [*] Waiting for messages. To exit press CTRL+C\n";
$callback = function ($msg) {
    echo ' [x] Received ', $msg->body, "\n";
    $database = new Medoo([
        'database_type' => 'mysql',
        'database_name' => 'gp',
        'server'        => '127.0.0.1',
        'username'      => 'root',
        'password'      => '0263d88b9e241e09'
    ]);

    $msg_data = json_decode($msg->body, true);
    $url = 'https://gupiao.baidu.com/api/stocks/stockweekbar?from=pc&os_ver=1&cuid=xxx&vv=100&format=json&stock_code='.$msg_data['f_code'].'&step=3&start=&count=160&fq_type=no&timestamp=' . time();
    $get_res = _curl_get($url);
    $get_res = json_decode($get_res, true);
    if ( ! $get_res) {
        return;
    }
    if ($get_res['mashData']) {
        foreach ($get_res['mashData'] as $k => $v) {
            $time_info = $database->select('t_price_week', ['id'], ['f_code' => $msg_data['f_code'], 'date' => $v['date'], 'time' => $v['time']]); 
            if (empty($time_info)) {
                $time_data = [
                    'f_code'         => (string) $msg_data['f_code'],
                    'kdj_d'          => (string) $v['kdj']['d'],
                    'kdj_j'          => (string) $v['kdj']['j'],
                    'kdj_k'          => (string) $v['kdj']['k'],
                    'kline_amount'   => (string) $v['kline']['amount'],
                    'kline_ccl'      => (string) $v['kline']['ccl'],
                    'kline_close'    => (string) $v['kline']['close'],
                    'kline_high'     => (string) $v['kline']['high'],
                    'kline_low'      => (string) $v['kline']['low'],
                    'kline_netChangeRatio'  => (string) $v['kline']['netChangeRatio'],
                    'kline_open'            => (string) $v['kline']['open'],
                    'kline_preClose'        => (string) $v['kline']['preClose'],
                    'kline_volume'          => (string) $v['kline']['volume'],
                    'ma5_avgPrice'          => (string) $v['ma5']['avgPrice'],
                    'ma5_ccl'               => (string) $v['ma5']['ccl'],
                    'ma5_volume'            => (string) $v['ma5']['volume'],
                    'ma10_avgPrice'         => (string) $v['ma10']['avgPrice'],
                    'ma10_ccl'              => (string) $v['ma10']['ccl'],
                    'ma10_volume'           => (string) $v['ma10']['volume'],
                    'ma20_avgPrice'         => (string) $v['ma20']['avgPrice'],
                    'ma20_ccl'              => (string) $v['ma20']['ccl'],
                    'ma20_volume'           => (string) $v['ma20']['volume'],
                    'macd_dea'              => (string) $v['macd']['dea'],
                    'macd_diff'             => (string) $v['macd']['diff'],
                    'macd_macd'             => (string) $v['macd']['macd'],
                    'rsi_rsi1'              => (string) $v['rsi']['rsi1'],
                    'rsi_rsi2'              => (string) $v['rsi']['rsi2'],
                    'rsi_rsi3'              => (string) $v['rsi']['rsi3']
                ];
                $database->insert('t_price_week', $time_data);
            } else {
                if ($time_info['date'] == date('Ymd')) {
                    $time_data = [
                        'f_code'         => (string) $msg_data['f_code'],
                        'kdj_d'          => (string) $v['kdj']['d'],
                        'kdj_j'          => (string) $v['kdj']['j'],
                        'kdj_k'          => (string) $v['kdj']['k'],
                        'kline_amount'   => (string) $v['kline']['amount'],
                        'kline_ccl'      => (string) $v['kline']['ccl'],
                        'kline_close'    => (string) $v['kline']['close'],
                        'kline_high'     => (string) $v['kline']['high'],
                        'kline_low'      => (string) $v['kline']['low'],
                        'kline_netChangeRatio'  => (string) $v['kline']['netChangeRatio'],
                        'kline_open'            => (string) $v['kline']['open'],
                        'kline_preClose'        => (string) $v['kline']['preClose'],
                        'kline_volume'          => (string) $v['kline']['volume'],
                        'ma5_avgPrice'          => (string) $v['ma5']['avgPrice'],
                        'ma5_ccl'               => (string) $v['ma5']['ccl'],
                        'ma5_volume'            => (string) $v['ma5']['volume'],
                        'ma10_avgPrice'         => (string) $v['ma10']['avgPrice'],
                        'ma10_ccl'              => (string) $v['ma10']['ccl'],
                        'ma10_volume'           => (string) $v['ma10']['volume'],
                        'ma20_avgPrice'         => (string) $v['ma20']['avgPrice'],
                        'ma20_ccl'              => (string) $v['ma20']['ccl'],
                        'ma20_volume'           => (string) $v['ma20']['volume'],
                        'macd_dea'              => (string) $v['macd']['dea'],
                        'macd_diff'             => (string) $v['macd']['diff'],
                        'macd_macd'             => (string) $v['macd']['macd'],
                        'rsi_rsi1'              => (string) $v['rsi']['rsi1'],
                        'rsi_rsi2'              => (string) $v['rsi']['rsi2'],
                        'rsi_rsi3'              => (string) $v['rsi']['rsi3']
                    ];
                    $database->update('t_prcei_week', $time_data, ['id' => $time_info['id']]);
                }
            }
        }
    }
};
$channel->basic_consume('gp_week', '', false, true, false, false, $callback);
while (count($channel->callbacks)) {
    $channel->wait();
}
$channel->close();
$connection->close();

function _curl_get($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 1);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}
